package com.springboot.controller;

import com.springboot.dto.AssistantMessageListView;
import com.springboot.model.AssistantMessageInform;
import com.springboot.model.AssistantMessageInformKey;
import com.springboot.model.MessageInform;
import com.springboot.model.UserLoginForm;
import com.springboot.service.AssistantMessageListService;
import com.springboot.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * 本控制台用于助理显示消息列表
 */
@Controller
public class AssistantMessageList {
    @Autowired
    UserLoginService userLoginService;
    @Autowired
    AssistantMessageListService assistantMessageListService;

    @CrossOrigin
    @RequestMapping(value = "/getAssistantInfoServlet")
    public @ResponseBody
    String assistantSeeMessageList(@RequestParam("username") String tel){
        try{
            System.out.println("开始看消息了");
            UserLoginForm userLoginForm=new UserLoginForm();
            userLoginForm=userLoginService.findUserByTel(tel);
            Integer assistantId=userLoginForm.getUserId();//assistantId
            String assistantName=userLoginForm.getUserName();
            String assistantTel=userLoginForm.getUserTel();
            ArrayList<AssistantMessageInform> assistantMessageInforms=new ArrayList<>();
            assistantMessageInforms=assistantMessageListService.getAssistantMessageInformById(assistantId);
            int num=assistantMessageInforms.size();
            ArrayList<AssistantMessageListView> assistantMessageListViews=new ArrayList<>();
            //显示3天之前的信息
            Calendar calendar1 = Calendar.getInstance();
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            calendar1.add(Calendar.DATE, -3);
            String threeDayAgo = sdf1.format(calendar1.getTime());
            Date curDate = new Date();
            String todayDate=sdf1.format(curDate);
            System.out.println("3天前的日期："+threeDayAgo+" || "+"今天的日期："+todayDate);
            for(int i=0;i<num;i++){
                int messageId=assistantMessageInforms.get(i).getMessageId();//消息ID
                String state=assistantMessageInforms.get(i).getMessageState3();//助理消息的读取状态
                MessageInform messageInform=new MessageInform();
                messageInform=assistantMessageListService.getMessageInformById(messageId);
                if(messageInform==null) {
                    continue;
                }
                String messageDate =sdf1.format(messageInform.getMessageDatetime());
                Integer infoType=messageInform.getMessageMark();
                SimpleDateFormat f1=new SimpleDateFormat("hh:mm:ss");
                String messageTime = f1.format(messageInform.getMessageDatetime());
                String appointDate=sdf1.format(messageInform.getBookDate());
                String appointTime=messageInform.getBookTime();
                Integer patientId=messageInform.getPatientId();
                Integer doctorId=messageInform.getDoctorId();
                UserLoginForm userLoginForm1=userLoginService.findUserById(patientId);
                String patientName=userLoginForm1.getUserName();
                String patientTel=userLoginForm1.getUserTel();
                userLoginForm1=userLoginService.findUserById(doctorId);
                String doctortName=userLoginForm1.getUserName();
                String doctorTel=userLoginForm1.getUserTel();

                AssistantMessageListView assistantMessageListView=new AssistantMessageListView();
                assistantMessageListView.setMessageId(messageId);
                assistantMessageListView.setMessageType(infoType);
                assistantMessageListView.setCurrDate(messageDate);
                assistantMessageListView.setCurrTime(messageTime);
                assistantMessageListView.setAppointDate(appointDate);
                assistantMessageListView.setAppointTime(appointTime);
                assistantMessageListView.setPatientName(patientName);
                assistantMessageListView.setPatientTel(patientTel);
                assistantMessageListView.setState(state);
                assistantMessageListView.setAssistantName(assistantName);
                assistantMessageListView.setAssistantTel(assistantTel);
                assistantMessageListView.setDoctorName(doctortName);
                assistantMessageListView.setDoctorTel(doctorTel);

                if((messageDate.compareTo(threeDayAgo)>=0)&&(messageDate.compareTo(todayDate)<=0)){
                    assistantMessageListViews.add(assistantMessageListView);
                }
                else if (state.equals("未读")){
                    assistantMessageListViews.add(assistantMessageListView);
                }
            }
            System.out.println("over");
            System.out.println(assistantMessageListViews.toString());

            return  assistantMessageListViews.toString();
        }
        catch (Exception e)
        {
            return "False";
        }
    }


    @CrossOrigin
    @RequestMapping(value = "/assistantSetReadServlet")
    public @ResponseBody
    String readMessageContent(@RequestParam("tel") String tel,
                              @RequestParam("infoId") Integer id){
        System.out.println("开始查看通知消息的内容");
        try {
            UserLoginForm userLoginForm=userLoginService.findUserByTel(tel);
            Integer assistantId=userLoginForm.getUserId();
            int result=0;
            System.out.println(assistantId+"|| "+id);
            AssistantMessageInformKey assistantMessageInformKey=new AssistantMessageInformKey(assistantId,id);
            result=assistantMessageListService.updateAssistantMessageInform( assistantMessageInformKey);
            System.out.println(result+" || "+"更新已读");
            return "true";
        }
        catch (Exception e){
            return "false";
        }
    }

    @CrossOrigin
    @RequestMapping(value = "/assistantSetAllReadServlet")
    public @ResponseBody
    String readWithOneButton(@RequestParam("assistantTel") String tel){
        System.out.println("开始看消息了");
        try{
            UserLoginForm userLoginForm=userLoginService.findUserByTel(tel);
            Integer assistantId=userLoginForm.getUserId();
            ArrayList<AssistantMessageInform> assistantMessageInforms=new ArrayList<>();
            assistantMessageInforms=assistantMessageListService.getAssistantMessageInformById(assistantId);
            int num=assistantMessageInforms.size();
            System.out.println("num"+num);
            for(int i=0;i<num;i++){
                if(assistantMessageInforms.get(i).getMessageState3().equals("未读")){
                    System.out.println(assistantMessageInforms.get(i).getMessageId()+" || "+num);
                    AssistantMessageInformKey assistantMessageInformKey=new AssistantMessageInformKey(assistantId,assistantMessageInforms.get(i).getMessageId());
                    assistantMessageListService.updateAssistantMessageInform(assistantMessageInformKey);
                }
            }
            return "true";
        }
        catch (Exception e){
            return "false";
        }
    }
}
